# -*- coding: utf-8 -*-
"""
 @Time    : 2021/1/19 下午5:30
 @Author  : tgy
 你有一个字典或者实例的序列，然后你想根据某个特定的字段比如 date 来分组迭 代访问。

"""
import itertools
from operator import itemgetter
from collections import defaultdict

rows = [
        {'address': '5412 N CLARK', 'date': '07/01/2012'},
        {'address': '5148 N CLARK', 'date': '07/04/2012'},
        {'address': '5800 E 58TH', 'date': '07/02/2012'},
        {'address': '2122 N CLARK', 'date': '07/03/2012'},
        {'address': '5645 N RAVENSWOOD', 'date': '07/02/2012'},
        {'address': '1060 W ADDISON', 'date': '07/02/2012'},
        {'address': '4801 N BROADWAY', 'date': '07/01/2012'},
        {'address': '1039 W GRANVILLE', 'date': '07/04/2012'},
    ]


def test01():
    rows.sort(key=itemgetter('date'))

    for date, items in itertools.groupby(rows, key=itemgetter('date')):
        ...
        print(date)
        for item in items:
            print(item)


def test02():
    ...
    dict = defaultdict(list)
    for row in rows:
        ...
        # dict.get(row['date'])
        dict[row['date']].append(row)
        # dict[rows['date']].append(row);

    print(dict)

if __name__ == '__main__':
    ...
    test02()
